package com.vision.shop.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.vision.shop.dao.IOrdersDao;
import com.vision.shop.dao.IProductDao;
import com.vision.shop.dao.IUserDao;
import com.vision.shop.model.Orders;
import com.vision.shop.model.Product;
import com.vision.shop.model.ShopCart;
import com.vision.shop.model.ShopDi;
import com.vision.shop.model.ShopException;
import com.vision.shop.model.User;

public class OrdersServlet extends BaseServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//注入ProductDao
	private IProductDao productDao;
	
	public IProductDao getProductDao() {
		return productDao;
	}
	@ShopDi
	public void setProductDao(IProductDao productDao) {
		this.productDao = productDao;
	}
	//注入UserDao 
	private IUserDao userDao;
	
	public IUserDao getUserDao() {
		return userDao;
	}
	@ShopDi
	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}
	//注入OrdersDao
	private IOrdersDao ordersDao;
	public IOrdersDao getOrdersDao() {
		return ordersDao;
	}
	@ShopDi
	public void setOrdersDao(IOrdersDao ordersDao) {
		this.ordersDao = ordersDao;
	}
	@Auth
	public String addToCart(HttpServletRequest req,HttpServletResponse resp){
			ShopCart shopCart = (ShopCart)req.getSession().getAttribute("shopCart");
			if(shopCart==null) {
				shopCart = new ShopCart();
				req.getSession().setAttribute("shopCart", shopCart);
			} 
			Product p = productDao.load(Integer.parseInt(req.getParameter("id")));
			try {
				shopCart.add(p);
			} catch (ShopException e) {
				return this.handleException(e, req);
			}
		
		return "redirect:/product.do?method=list";
	}
	
	@Auth
	public String showCart(HttpServletRequest req,HttpServletResponse resp){
		User u=(User)req.getSession().getAttribute("loginUser");
		req.setAttribute("addresses", userDao.load(u.getId()).getAddresses());
		return "orders/showCart.jsp";
	}
	@Auth
	public String deleteProduct(HttpServletRequest req,HttpServletResponse resp){
		int pid=Integer.parseInt(req.getParameter("pid"));
		ShopCart shopCart = (ShopCart)req.getSession().getAttribute("shopCart");
		if(shopCart!=null){
			shopCart.delete(pid);
		}
		return "redirect:/orders.do?method=showCart";
	}
	
	@Auth
	public String clearShopCart(HttpServletRequest req,HttpServletResponse resp){
		ShopCart shopCart = (ShopCart)req.getSession().getAttribute("shopCart");
		if(shopCart!=null) {
			shopCart.clearShopCart();
		} 
		return "redirect:/orders.do?method=showCart";
	}
	
	@Auth
	public String productAddNumberInput(HttpServletRequest req,HttpServletResponse resp){
		req.setAttribute("pid",Integer.parseInt(req.getParameter("pid")));
		return "orders/productAddNumberInput.jsp";
	}
	@Auth
	public String productAddNumber(HttpServletRequest req,HttpServletResponse resp){
			int pid=Integer.parseInt(req.getParameter("pid"));
		try {
			int number=Integer.parseInt(req.getParameter("number"));
			//如果没有抛异常继续执行
			ShopCart shopCart = (ShopCart)req.getSession().getAttribute("shopCart");
			if(shopCart!=null) {
				shopCart.addProductNumber(pid,number);
			} 
		} catch (NumberFormatException e) {
			this.getErrors().put("number","数量必须为整数");
			return "orders/productAddNumberInput.jsp";
		}catch (ShopException e) {
			req.setAttribute("pid",pid);
			return this.handleException(e, req);
		}
		return "redirect:/orders.do?method=showCart";
	}
	@Auth
	public String addOrders(HttpServletRequest req,HttpServletResponse resp){
		int aid=Integer.parseInt(req.getParameter("address"));
		double price=Double.parseDouble(req.getParameter("totalPrice"));
		Orders o=new Orders();
		o.setBuyDate(new Date());
		o.setStatus(1);
		o.setPrice(price);
		User u=(User)req.getSession().getAttribute("loginUser");
		//添加
		ordersDao.add(o, u, aid,((ShopCart)req.getSession().getAttribute("shopCart")).getProducts());
		return "redirect:/user.do?method=show&id="+u.getId();
	}
	
	
}
